# -*- coding: utf-8 -*-..,
##############################################################################
#
#    @author: PhongND
#    Copyright (C) 2014 Crea8s (http://www.crea8s.com) All Rights Reserved.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

# from openerp.osv import fields, osv
# from openerp.tools.translate import _
# from openerp import tools
# ===== =====
# import pytz
# import tzlocal
# from tzlocal import get_localzone # $ pip install tzlocal
# ===== =====
# from xmlrpclib import ServerProxy, Error

# -*- coding: utf-8 -*-
"""
    api

    Extends magento python api

    :copyright: (c) 2014 crea8s
    :license: BSD, see LICENSE for more details.
"""
from magento.api import API


class Core(API):
    """
    This API extends the API for the custom API implementation
    for the magento extension
    
    """

    __slots__ = ()

    def websites(self):
        """
        Returns list of all websites        
        
        :author phongnd
        :created_date: 20140909
        """
        return self.call('ol_websites.list', [])

    def stores(self, filters=None):
        """
        Returns list of all group store
        
        :author phongnd
        :created_date: 20140909
        :param filters: Dictionary of filters.

               Format :
                   {<attribute>:{<operator>:<value>}}
               Example :
                   {'website_id':{'=':'1'}}
        :return: List of Dictionaries
        """
        return self.call('ol_groups.list', [filters])

    def store_views(self, filters=None):
        """
        Returns list of all store views
        
        :author phongnd
        :created_date: 20140909
        :param filters: Dictionary of filters.

               Format :
                   {<attribute>:{<operator>:<value>}}
               Example :
                   {'website_id':{'=':'1'}}
        :return: List of Dictionaries
        """
        return self.call('ol_storeviews.list', [filters])


class OrderConfig(API):
    '''
    Getting Order Configuration from magento.
    '''

    def get_states(self):
        """
        Get states of orders

        :author phongnd
        :created_date: 20140909
        :return: dictionary of all states.
                 Format :
                    {<state>: <state title>}
                 Example :
                    {   'canceled': 'Canceled',
                        'closed': 'Closed',
                        'holded': 'On Hold',
                        'pending_payment': 'Pending Payment'
                    }
        """
        return self.call('sales_order.get_order_states', [])

    def get_shipping_methods(self):
        """
        Get available shipping methods.

        :author phongnd
        :created_date: 20140909
        :return: List of dictionaries of all available shipping method.
                 Example :
                         [
                            {'code': 'flatrate', 'label': 'Flat Rate'},
                            {'code': 'tablerate', 'label': 'Best Way'},
                            ...
                         ]
        """
        return self.call('sales_order.shipping_methods', [])


